package data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class CounterPayment extends AbstractData{
	private long id = -1;
	private long hotelId;
	private Counter counter;
	private String counterString;
	private double openAmount;
	private Date openDate;
	private Date openNightAuditDate;
	private Date closeDate;
	private Date closeNightAuditDate;
	private List<Payment> payment;
	private String paymentString;
	private double totalAmount;
	
	public CounterPayment(){}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getHotelId() {
		return hotelId;
	}
	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}
	
	public Counter getCounter() {
		if (counter == null
				&& !(counterString == null || counterString.isEmpty())) {
			Counter p = new Counter(Long.parseLong(counterString));
			p.LoadData();
			counter = p;
		}
		return counter;
	}
	public void setCounter(Counter counter) {
		this.counterString = String.format("%d", counter.getId());
		this.counter = counter;
	}
	
	public String getCounterString() {
		return counterString;
	}
	public void setCounterString(String counterString) {
		this.counterString = counterString;
	}
	
	public double getOpenAmount() {
		return openAmount;
	}
	public void setOpenAmount(double openAmount) {
		this.openAmount = openAmount;
	}
	
	public Date getOpenDate() {
		return openDate;
	}
	public void setOpenDate(Date openDate) {
		this.openDate = openDate;
	}
	
	public Date getOpenNightAuditDate() {
		return openNightAuditDate;
	}
	public void setOpenNightAuditDate(Date openNightAuditDate) {
		this.openNightAuditDate = openNightAuditDate;
	}
	
	public Date getCloseDate() {
		return closeDate;
	}
	public void setCloseDate(Date closeDate) {
		this.closeDate = closeDate;
	}
	
	public Date getCloseNightAuditDate() {
		return closeNightAuditDate;
	}
	public void setCloseNightAuditDate(Date closeNightAuditDate) {
		this.closeNightAuditDate = closeNightAuditDate;
	}
	
	public List<Payment> getPayment() {
		if ((payment == null || payment.size() == 0) && !(paymentString == null || paymentString.isEmpty())) {
			String[] idOfList = SplitList(paymentString);
			List<Payment> list = new ArrayList<Payment>();
			for (String id : idOfList) {
				Payment c = new Payment(Long.parseLong(id));
				list.add(c);
			}
			
			for(Payment c : list)
				c.LoadData();
			
			payment = list;
		}
		return payment;
	}

	public void setPayment(List<Payment> payment) {
		this.paymentString = CombineList(payment);
		this.payment = payment;
	}
	
	public String getPaymentString() {
		return paymentString;
	}
	public void setPaymentString(String paymentString) {
		this.paymentString = paymentString;
	}
	public double getTotalAmount() {
		return totalAmount;
	}
	public void setTotalAmount(double totalAmount) {
		this.totalAmount = totalAmount;
	}
	
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.CounterPayment);
		else
			item = new Entity(this.key);		
			item.setProperty("hotelId", this.hotelId);
			item.setProperty("counter", this.counterString);
			item.setProperty("openAmount", this.openAmount);
			item.setProperty("openDate", this.openDate);
			item.setProperty("openNightAuditDate", this.openNightAuditDate);
			item.setProperty("closeDate", this.closeDate);
			item.setProperty("closeNightAuditDate", this.closeNightAuditDate);
			item.setProperty("payment", this.paymentString);
			item.setProperty("totalAmount", this.totalAmount);
		return item;
	}

	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long)entity.getProperty("hotelId");
		this.counterString = (String)entity.getProperty("counter");
		
		if(entity.getProperty("openAmount") == null){
			this.openAmount = 0;
		}else{
			this.openAmount = (Double)entity.getProperty("openAmount");
		}
		
		this.openDate = (Date)entity.getProperty("openDate");
		this.openNightAuditDate = (Date)entity.getProperty("openNightAuditDate");
		this.closeDate = (Date)entity.getProperty("closeDate");
		this.closeNightAuditDate = (Date)entity.getProperty("closeNightAuditDate");
		this.paymentString = (String)entity.getProperty("payment");
		
		if(entity.getProperty("totalAmount") == null){
			this.totalAmount = 0;
		}else{
			this.totalAmount = (Double)entity.getProperty("totalAmount");
		}
		
		this.key = entity.getKey();
	}

	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.CounterPayment, this.id);
			setEntity(entity);
		}
	}
}
